package com.syqc.monitor.ui.alarm

import android.view.View
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.github.mikephil.charting.charts.BarChart
import com.github.mikephil.charting.data.BarEntry
import com.syqc.adapter.AlarmHistoryAdapter
import com.syqc.comlib.base.BaseToolbarRecycleActivity
import com.syqc.comlib.utils.ToolbarHelper
import com.syqc.comlib.utils.showLongToast
import com.syqc.comlib.utils.startActivityTo
import com.syqc.entity.HistoryAlarm
import com.syqc.monitor.R
import com.syqc.monitor.chart.MPChartUtils
import com.syqc.monitor.databinding.HeadAdasInfoBinding
import com.syqc.utils.DataUtil
import com.syqc.utils.Keys
import com.syqc.utils.ToolsUtil

/**
 *@Author Jasper
 *@Time   2020/12/9
 *@Desc   主动安全报警列表
 */
class AdasInfoActivity : BaseToolbarRecycleActivity() {
    private var carId = ""
    private var title = ""

    private val mAdapter by lazy { AlarmHistoryAdapter() }
    override fun initView() {
        super.initView()
        title = intent.getStringExtra(Keys.INTENT_TITLE) ?: ""
        carId = intent.getStringExtra(Keys.INTENT_CAR_ID) ?: ""
        ToolbarHelper.setToolbar(this, title)
    }

    override fun initRecyclerView() {
        mRecycler.adapter = mAdapter
        mAdapter.addHeaderView(initHeadView())
        mAdapter.setList(DataUtil.alarmEntity?.list)
    }

    override fun initHeadView(): View {
        val binding = HeadAdasInfoBinding.inflate(layoutInflater)

        initChart(DataUtil.alarmEntity, binding.mBarChart)
        return binding.root
    }

    private fun initChart(entity: HistoryAlarm?, mBarChart: BarChart) {
        if (entity == null) return
        val entries = ArrayList<BarEntry>()
        val xLabe = ArrayList<String>()
        for ((index, infoEntity) in entity.list.withIndex()) {
            entries.add(BarEntry(index.toFloat(), infoEntity.total.toFloat()))
            xLabe.add("${index + 1}")
        }

        MPChartUtils.configBarChart(mBarChart, xLabe)
        MPChartUtils.initBarChart(mBarChart, entries, "", barWidth = 0.7f)
    }

    override fun setListener() {
        mAdapter.setOnItemClickListener { adapter, view, position ->
            if (ToolsUtil.isFastClick()) return@setOnItemClickListener
            val info =
                adapter.data[position] as HistoryAlarm.TotalAlarms
            if (info.total == 0) {
                showLongToast(R.string.toast_no_about_alarm)
            } else {
                startActivityTo<AlarmDetailActivity>(this) {
                    putExtra(Keys.INTENT_CAR_ID, carId)
                    putExtra(Keys.INTENT_PLATE, title)
                    putExtra(Keys.INTENT_TITLE, "${info.name}(${info.total})")
                    DataUtil.alarmInfo = info.list
                }
            }
        }

        binding.tvToTop.setOnClickListener {
            mRecycler.smoothScrollToPosition(0)
        }

        mRecycler.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                val position = mLayoutManger.findFirstVisibleItemPosition()
                binding.tvToTop.isVisible = position > 0
            }
        })
    }
}